package com.prolestar.territorialMedia.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.prolestar.common.core.domain.AjaxResult;
import com.prolestar.common.core.domain.model.LoginUser;
import com.prolestar.common.utils.DateUtils;
import com.prolestar.common.utils.SecurityUtils;
import com.prolestar.system.service.ISysRoleService;
import com.prolestar.territorialMedia.domain.PsVmediaAcountinfo;
import com.prolestar.territorialMedia.domain.PsVmediaPerson;
import com.prolestar.territorialMedia.domain.SysArea;
import com.prolestar.territorialMedia.domain.TerritorialMediaDTO;
import com.prolestar.territorialMedia.mapper.TerritorialMediaMapper;
import com.prolestar.territorialMedia.service.IterritorialMediaService;
import com.prolestar.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@Service
public class TerritorialMediaService implements IterritorialMediaService {
    @Autowired
    private TerritorialMediaMapper territorialMediaMapper;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisUtils redisUtils;


    /**
     * 缓存省份
     */
    @PostConstruct
    public void areas(){
        // 清理缓存
        redisUtils.del("areas");
        List<SysArea> list = territorialMediaMapper.areaList();
        redisTemplate.opsForValue().set("areas",list);
    }

    /**
     * 查询所有省市县
     * @return
     */
    @Override
    public Object areaList() {
        Object areas = redisTemplate.opsForValue().get("areas");
        return areas;
    }





    /**
     * 查询自媒体列表
     * @param psVmediaPerson
     * @return
     */
    @Override
    public List<PsVmediaPerson> mediaList(PsVmediaPerson psVmediaPerson) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        Set<String> permission = sysRoleService.selectRolePermissionByUserId(userId);
        psVmediaPerson.setUserId(userId);
        if (permission.contains("admin")||permission.contains("conservator")){
            List<PsVmediaPerson> mediaList = territorialMediaMapper.mediaList(psVmediaPerson);
            particularsList(mediaList);
            return mediaList;
        }else {
            List<PsVmediaPerson> mediaList = territorialMediaMapper.mediaUserList(psVmediaPerson);
            particularsList(mediaList);
            return mediaList;
        }
    }

    /**
     * 查询自媒体详细信息
     * @param id
     * @return
     */
    @Override
    public PsVmediaPerson selectPsVmediaPersonById(Long id) {
        PsVmediaPerson psVmediaPerson = territorialMediaMapper.selectPsVmediaPersonById(id);
        List<PsVmediaAcountinfo> mediumInformationList =  territorialMediaMapper.mediumInformationList(psVmediaPerson.getId());
        for (PsVmediaAcountinfo t : mediumInformationList){
            PsVmediaAcountinfo acountinfo = new PsVmediaAcountinfo();
            switch (t.getVmediaName()){
                case 1:
                    acountinfo.setId(t.getId());
                    acountinfo.setVmediaNickname(t.getVmediaNickname());
                    acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                    acountinfo.setVmediaCreations(t.getVmediaCreations());
                    acountinfo.setVmediaName(t.getVmediaName());
                    acountinfo.setVmediaFans(t.getVmediaFans());
                    psVmediaPerson.setDouYinList(acountinfo);
                    break;
                case 2:
                    acountinfo.setId(t.getId());
                    acountinfo.setVmediaNickname(t.getVmediaNickname());
                    acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                    acountinfo.setVmediaCreations(t.getVmediaCreations());
                    acountinfo.setVmediaName(t.getVmediaName());
                    acountinfo.setVmediaFans(t.getVmediaFans());
                    psVmediaPerson.setBiLiList(acountinfo);
                    break;
                case 3:
                    acountinfo.setId(t.getId());
                    acountinfo.setVmediaNickname(t.getVmediaNickname());
                    acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                    acountinfo.setVmediaCreations(t.getVmediaCreations());
                    acountinfo.setVmediaName(t.getVmediaName());
                    acountinfo.setVmediaFans(t.getVmediaFans());
                    psVmediaPerson.setKuaiShouList(acountinfo);
                    break;
                case 4:
                    acountinfo.setId(t.getId());
                    acountinfo.setVmediaNickname(t.getVmediaNickname());
                    acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                    acountinfo.setVmediaCreations(t.getVmediaCreations());
                    acountinfo.setVmediaName(t.getVmediaName());
                    acountinfo.setVmediaFans(t.getVmediaFans());
                    psVmediaPerson.setWeiBoList(acountinfo);
                    break;
                case 5:
                    acountinfo.setId(t.getId());
                    acountinfo.setVmediaNickname(t.getVmediaNickname());
                    acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                    acountinfo.setVmediaCreations(t.getVmediaCreations());
                    acountinfo.setVmediaName(t.getVmediaName());
                    acountinfo.setVmediaFans(t.getVmediaFans());
                    psVmediaPerson.setHuoShanList(acountinfo);
                    break;
                case 6:
                    acountinfo.setId(t.getId());
                    acountinfo.setVmediaNickname(t.getVmediaNickname());
                    acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                    acountinfo.setVmediaCreations(t.getVmediaCreations());
                    acountinfo.setVmediaName(t.getVmediaName());
                    acountinfo.setVmediaFans(t.getVmediaFans());
                    psVmediaPerson.setWeiXinList(acountinfo);
                    break;
            }
        }
        return psVmediaPerson;
    }

    /**
     * 自媒体新增
     * @param territorialMediaDTO
     * @return
     */
    @Override
    @Transactional
    public AjaxResult insertPsVmediaPerson(TerritorialMediaDTO territorialMediaDTO) {
        territorialMediaDTO.setIsDelete(0); // 默认不删除
        territorialMediaDTO.setCreateTime(DateUtils.getNowDate());
        territorialMediaDTO.setCreateBy(SecurityUtils.getUserId());
        territorialMediaDTO.setIsFocus(0);  // 默认不关注
        territorialMediaDTO.setUserId(SecurityUtils.getUserId());
        List<PsVmediaAcountinfo> list = territorialMediaDTO.getPsVmediaAcountinfos();
        territorialMediaMapper.insertPsVmediaPerson(territorialMediaDTO);
            for (PsVmediaAcountinfo l : list){
                l.setVmediaPersonId(territorialMediaDTO.getId());
                l.setIsDelete(0);  // 默认不删除
                territorialMediaMapper.insertPsVmediaAcountinfo(l);
            }
        return AjaxResult.success();

    }

    /**
     * 修改自媒体
     * @param territorialMediaDTO
     * @return
     */
    @Override
    public AjaxResult updatePsVmediaPerson(TerritorialMediaDTO territorialMediaDTO) {
        territorialMediaDTO.setUpdateBy(SecurityUtils.getUserId());
        territorialMediaDTO.setUpdateTime(DateUtils.getNowDate());
        territorialMediaMapper.updatePsVmediaPerson(territorialMediaDTO);
        List<PsVmediaAcountinfo> list = territorialMediaDTO.getPsVmediaAcountinfos();
         List<Long>  mediaName = territorialMediaMapper.selectPsCompanypartyActivitesByid(territorialMediaDTO.getId());

        if (territorialMediaDTO.getPsVmediaAcountinfos() != null){
            list.forEach(l->{
                if (mediaName.contains(l.getVmediaName())){
                    PsVmediaAcountinfo acountinfo = new PsVmediaAcountinfo();
                    acountinfo.setVmediaPersonId(territorialMediaDTO.getId());
                    acountinfo.setVmediaName(l.getVmediaName());
                    acountinfo.setVmediaNickname(l.getVmediaNickname());
                    acountinfo.setVmediaAcountId(l.getVmediaAcountId());
                    acountinfo.setVmediaCreations(l.getVmediaCreations());
                    acountinfo.setVmediaFans(l.getVmediaFans());
                    territorialMediaMapper.updatePsVmediaAcountinfo(acountinfo);
                }else {
                    l.setVmediaPersonId(territorialMediaDTO.getId());
                    l.setIsDelete(0);  // 默认不删除
                    territorialMediaMapper.insertPsVmediaAcountinfo(l);
                }
            });
        }
        return AjaxResult.success();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public AjaxResult deletePsVmediaPerson(Long[] ids) {
        for (Long id : ids){
            territorialMediaMapper.deletePsVmediaPerson(id);
        }
        return AjaxResult.success();
    }

    /**
     * 关注
     * @param psVmediaPerson
     * @return
     */
    @Override
    public AjaxResult UpdateAttention(PsVmediaPerson psVmediaPerson) {
        if (psVmediaPerson.getIsFocus() == 0){
                psVmediaPerson.setIsFocus(1);  // 修改为关注
                territorialMediaMapper.UpdateAttention(psVmediaPerson);
        }else {
            psVmediaPerson.setIsFocus(0);
            territorialMediaMapper.UpdateAttention(psVmediaPerson);
        }
        return AjaxResult.success();
    }


    /**
     * 媒体信息
     * @param mediaList
     */
    public void particularsList(List<PsVmediaPerson> mediaList){
        mediaList.forEach(m->{
            Long id = m.getId();
            List<PsVmediaAcountinfo> mediumInformationList =  territorialMediaMapper.mediumInformationList(id);
            for (PsVmediaAcountinfo t : mediumInformationList){
                PsVmediaAcountinfo acountinfo = new PsVmediaAcountinfo();
                switch (t.getVmediaName()){
                    case 1:
                        acountinfo.setId(t.getId());
                        acountinfo.setVmediaNickname(t.getVmediaNickname());
                        acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                        acountinfo.setVmediaCreations(t.getVmediaCreations());
                        acountinfo.setVmediaName(t.getVmediaName());
                        acountinfo.setVmediaFans(t.getVmediaFans());
                        m.setDouYinList(acountinfo);
                        break;
                    case 2:
                        acountinfo.setId(t.getId());
                        acountinfo.setVmediaNickname(t.getVmediaNickname());
                        acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                        acountinfo.setVmediaCreations(t.getVmediaCreations());
                        acountinfo.setVmediaName(t.getVmediaName());
                        acountinfo.setVmediaFans(t.getVmediaFans());
                        m.setBiLiList(acountinfo);
                        break;
                    case 3:
                        acountinfo.setId(t.getId());
                        acountinfo.setVmediaNickname(t.getVmediaNickname());
                        acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                        acountinfo.setVmediaCreations(t.getVmediaCreations());
                        acountinfo.setVmediaName(t.getVmediaName());
                        acountinfo.setVmediaFans(t.getVmediaFans());
                        m.setKuaiShouList(acountinfo);
                        break;
                    case 4:
                        acountinfo.setId(t.getId());
                        acountinfo.setVmediaNickname(t.getVmediaNickname());
                        acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                        acountinfo.setVmediaCreations(t.getVmediaCreations());
                        acountinfo.setVmediaName(t.getVmediaName());
                        acountinfo.setVmediaFans(t.getVmediaFans());
                        m.setWeiBoList(acountinfo);
                        break;
                    case 5:
                        acountinfo.setId(t.getId());
                        acountinfo.setVmediaNickname(t.getVmediaNickname());
                        acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                        acountinfo.setVmediaCreations(t.getVmediaCreations());
                        acountinfo.setVmediaName(t.getVmediaName());
                        acountinfo.setVmediaFans(t.getVmediaFans());
                        m.setHuoShanList(acountinfo);
                        break;
                    case 6:
                        acountinfo.setId(t.getId());
                        acountinfo.setVmediaNickname(t.getVmediaNickname());
                        acountinfo.setVmediaAcountId(t.getVmediaAcountId());
                        acountinfo.setVmediaCreations(t.getVmediaCreations());
                        acountinfo.setVmediaName(t.getVmediaName());
                        acountinfo.setVmediaFans(t.getVmediaFans());
                        m.setWeiXinList(acountinfo);
                        break;
                }
            }
        });
    }

}
